
/**********************************************************************
***********************************************************************
* FILE		: main.C
* Description	: 
*			  
* Copyright (c) 2013 by BTCS. All Rights Reserved.
* 
* History:
* Version		Name       		Date			Description
   0.1		AlexZhang	2013/01/02	Initial Version
   
***********************************************************************
**********************************************************************/



#include    "includes.h"

#define SLAVE_ADDRESS 0x01
#define UART_PORT 1
#define BOUDRATE	19200


void main (void)
{
    eMBErrorCode    eStatus;
    eBSPUSART1 type;
    IBUS_DATA eData;
    USHORT usVol = 0;
    
    INTCONbits.GIEH = 0;
    INTCONbits.GIEL = 1;//enable all peripheral interrupt
    RCONbits.IPEN = 0;//disable priority levels on interrupts(PIC16 Compatibility mode)
    IPR1 = 0;
    
        
    // configure the input/output pins
    ADCON1=0x0f;  //config all pin as gpio,default is analog input channel
    CMCON=0x07;//config all pin as gpio,default is comparator
    MEMCONbits.EBDIS = 1;//external system bus disabled,all external bus drivers are mapped as I/O ports   

    //OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_4);
    //WriteTimer0(-10000);

    type = RS2321;
    xConfigUSART1(type);
    
     eStatus = eMBInit( MB_RTU, SLAVE_ADDRESS, UART_PORT, BOUDRATE, MB_PAR_ODD);

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );

     INTCONbits.GIEH = 1;//enalble interrupt
    INTCONbits.TMR0IE = 1;
    
    IbusInit();
    eData.cmd = 0x01;
    for(;;)
    {
        usVol++;
        if(usVol >0xfff)
            usVol = 0;
        
        eData.data1 = usVol&0x00ff;
        eData.data2 = (usVol&0x0f00)>>8;
        eData.data3 = 0;
        eData.data4 = 0;
        IbusWrite(&eData);
        DAC_EN = 0;
        Delay10KTCYx(10);
        DAC_EN = 1;
        eMBPoll();
    }
}

